/**
 *****************************************************************************
 * DESC ：. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-8-4 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-8-4		Created.
 ****************************************************************************
 **/
package com.me.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;

/**
 * 
 * <br/>
 * 下午6:16:26 2012-8-4
 * 
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class JTalkFrontCommand extends JFrame implements JTalkClientCommandInterface {

	private static final long serialVersionUID = 1L;

	private JTalkClientFront parent = null;
	private JPanel jPanelTop = null;
	private JPanel jPanelCenter = null;
	private JPanel jPanelBottom = null;
	private JComboBox jComboBoxCommand = null;
	private JButton jButtonSend = null;
	private JPanel jPanelCheck = null;
	private JCheckBox jCheckBoxFormat = null;
	private JButton jButtonClose = null;
	private JScrollPane jScrollPaneCenter = null;
	private JTextPane jTextPaneCenter = null;
	private boolean methodsLoaded = false;

	private String lastResponse = null;

	public JTalkFrontCommand(JTalkClientFront parent) {
		this.parent = parent;
		this.initComponents();
		this.setLocationRelativeTo(parent);
		this.parent.theClient.registerForCommandResponse(this);
	}

	/**
	 * 初始化组件.
	 */
	private void initComponents() {

		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.jPanelTop = new JPanel();
		this.jComboBoxCommand = new JComboBox();
		this.jComboBoxCommand.setEditable(true);
		this.jComboBoxCommand
				.setModel(new DefaultComboBoxModel(new String[] { "No methods available. Connect first!" }));
		this.jComboBoxCommand.setPreferredSize(new Dimension(350, 24));
		this.jPanelTop.add(jComboBoxCommand);
		this.jButtonSend = new JButton();
		this.jButtonSend.setText("Send");
		this.jButtonSend.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				sendCommand(e);
			}
		});
		this.jButtonSend.getAccessibleContext().setAccessibleName("jButtonSend");
		this.jPanelTop.add(jButtonSend);
		this.getContentPane().add(jPanelTop, BorderLayout.NORTH);

		this.jPanelBottom = new JPanel();
		this.jPanelBottom.setLayout(new BorderLayout());
		this.jPanelCheck = new JPanel();
		this.jPanelCheck.setLayout(new BorderLayout());
		this.jCheckBoxFormat = new JCheckBox();
		this.jCheckBoxFormat.setText("Format output");
		this.jCheckBoxFormat.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				formatOutput();
			}
		});
		this.jPanelCheck.add(jCheckBoxFormat, BorderLayout.CENTER);
		this.jPanelBottom.add(jPanelCheck, BorderLayout.CENTER);
		this.jButtonClose = new JButton();
		this.jButtonClose.setText("Close");
		this.jButtonClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				closeDialog();
			}
		});
		this.jPanelBottom.add(jButtonClose, BorderLayout.EAST);
		this.getContentPane().add(jPanelBottom, BorderLayout.SOUTH);

		this.jPanelCenter = new JPanel();
		this.jPanelCenter.setLayout(new BorderLayout());
		this.jPanelCenter.setPreferredSize(new Dimension(10, 240));
		this.jTextPaneCenter = new JTextPane();
		this.jScrollPaneCenter = new JScrollPane();
		this.jScrollPaneCenter.setMinimumSize(new Dimension(220, 220));
		this.jScrollPaneCenter.setViewportView(this.jTextPaneCenter);
		this.jPanelCenter.add(jScrollPaneCenter, BorderLayout.CENTER);
		this.getContentPane().add(jPanelCenter, BorderLayout.CENTER);

		this.pack();
	}

	@Override
	public void setVisible(boolean visible) {
		if (!this.methodsLoaded
				&& this.parent.theClient.sendMessage("std::getMethods()", null, "command", null, "getmethods01")) {

		}
		super.setVisible(visible);
	}

	@Override
	public boolean setCommandAnswer(String messageID, String sender, String content) {
		// TODO Auto-generated method stub
		if (messageID.equals("getmethods01")) {
			if (content == null) {
				return false;
			}
			String[] methods = content.split(";;");
			this.jComboBoxCommand.setModel(new DefaultComboBoxModel(methods));
			this.methodsLoaded = true;
			return true;
		}else if(messageID.equals("usercommand01")){
			this.lastResponse  = content;
			if(this.lastResponse != null){
				this.showResponse();
			}
			return true;
		}
		return false;
	}

	/**
	 * 显示信息.
	 */
	private void showResponse() {
		if(this.lastResponse == null){
			return;
		}
		if(this.jCheckBoxFormat.isSelected()){
			this.jTextPaneCenter.setText(this.lastResponse.replaceAll(";;", "\n"));
		}else{
			this.jTextPaneCenter.setText(this.lastResponse);
		}
	}

	/**
	 * 向服务器发送命令.
	 * 
	 * @param e
	 */
	private void sendCommand(ActionEvent e) {
		if (!this.parent.theClient.isConnected) {
			return;
		}
		this.parent.theClient.sendMessage((String) this.jComboBoxCommand.getSelectedItem(), null, "command", null,
				"usercommand01");
	}
	
	/**
	 * 格式化输出.
	 */
	private void formatOutput() {
		this.showResponse();
	}
	
	/**
	 * 关闭命令框.
	 */
	private void closeDialog() {
		this.dispose();
	}

}
